package com.adcc.ags.gms.core.service;

import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.entity.SysUser;
import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.gw.service.SysConfigurationService;
import com.adcc.utility.codec.MD5;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 系统登录实现层
 */
@Transactional
@Service("LoginService")
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SysConfigurationService sysConfigurationService;

    @Autowired
    private LogFactory log;

    @Override
    public Optional<ModelAndView> verifyUser(SysUser user, HttpSession session) {
        ModelAndView view = new ModelAndView();
        try {
            if (user.getType() == 0) {//管理员用户
                if (sysConfigurationService.getValue(Constant.ADMIN_USER_NAME).equals(user.getName())) {
                    if (sysConfigurationService.getValue(Constant.ADMIN_USER_PWD).equals(MD5.getMD5(user.getPassword()))) {
                        session.setAttribute("user", new SysUser(3, user.getName(),null,user.getType()));
                        session.setAttribute("loginTime", new Date().getTime());
                        return Optional.of(view);
                    } else {
                        view.addObject("type", user.getType());
                        view.addObject("error", "密码错误");
                        return Optional.of(view);
                    }
                } else {
                    view.addObject("type", user.getType());
                    view.addObject("error", "用户名不存在");
                    return Optional.of(view);
                }
            } else {//监控用户
                if (sysConfigurationService.getValue(Constant.MONITOR_USER_NAME).equals(user.getName())) {
                    if (sysConfigurationService.getValue(Constant.MONITOR_USER_PWD).equals(MD5.getMD5(user.getPassword()))) {
                        session.setAttribute("user", new SysUser(1, user.getName(),null, user.getType()));
                        session.setAttribute("loginTime", new Date().getTime());
                        return Optional.of(view);
                    } else {
                        view.addObject("type", user.getType());
                        view.addObject("error", "密码错误");
                        return Optional.of(view);
                    }
                } else {
                    view.addObject("type", user.getType());
                    view.addObject("error", "用户名不存在");
                    return Optional.of(view);
                }
            }
        } catch (Exception e) {
            log.error(LoginService.class, "Login", GWLogLevel.ALARM, "验证登录用户失败", e);
            return Optional.absent();
        }
    }
}
